package dbhandler;

import evthandler.EventMessage;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author Casper, Dennis, Jackie, Jakob
 * @version 1.00
 */
public class Guest {

    private int guestNo;
    private String firstName;
    private String middleName;
    private String lastName;
    private String passportNo;
    private String email;
    private String phone1No;
    private String phone2No;
    private Address address;
    private Prefix prefix;
    private boolean businessGuest;
    private Company company;
    private Room lastRoom;

    /**
     *
     */
    public Guest() {
    }

    /**
     * Make a new Guest object
     * @param guestNo
     */
    public Guest(int guestNo) {
        this.guestNo = guestNo;
        int addressNo = 0;
        String roomNo = "";
        int companyNo = 0;
        int prefixNo = 0;
        try {
            ResultSet rs = DbCon.getInstance().executeQuery("SELECT * FROM guest WHERE guestNo=" + guestNo);
            while (rs.next()) {
                firstName = rs.getString("firstName");
                middleName = rs.getString("middleName");
                lastName = rs.getString("lastName");
                passportNo = rs.getString("passportNo");
                email = rs.getString("email");
                phone1No = rs.getString("phone1No");
                phone2No = rs.getString("phone2No");
                businessGuest = rs.getBoolean("isBusinessGuest");
                addressNo = rs.getInt("addressNo");
                roomNo = rs.getString("lastRoomNo");
                companyNo = rs.getInt("companyNo");
                prefixNo = rs.getInt("prefixNo");
            }
        } catch (SQLException ex) {
            new EventMessage("SQL error in Guest", "There was an error performing the sql inquery\n").showException(ex);
        }
        address = new Address(addressNo);
        prefix = new Prefix(prefixNo);
        company = new Company(companyNo);
        lastRoom = new Room(roomNo);
    }

    /**
     *
     * @return
     */
    public boolean exists(int guestNo) {
        boolean exists = false;
        try {
            ResultSet rs = DbCon.getInstance().executeQuery("SELECT count(guestNo) FROM guest WHERE guestNo=" + guestNo);
            while (rs.next()) {
                if (rs.getInt(1) > 0) {
                    exists = true;
                }
            }
        } catch (SQLException ex) {
            new EventMessage("SQL error in Guest.exists()", "There was an error performing the sql inquery\n").showException(ex);
        }

        return exists;
    }

    public boolean isBusinessGuest() {
        return businessGuest;
    }

    /**
     *
     * @return
     */
    public int getGuestNo() {
        return guestNo;
    }

    /**
     *
     * @param address
     */
    public void setAddress(Address address) {
        this.address = address;
    }

    /**
     *
     * @param businessGuest
     */
    public void setBusinessGuest(boolean businessGuest) {
        this.businessGuest = businessGuest;
    }

    /**
     *
     * @param company
     */
    public void setCompany(Company company) {
        this.company = company;
    }

    /**
     *
     * @param firstName
     */
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    /**
     *
     * @param guestNo
     */
    public void setGuestNo(int guestNo) {
        this.guestNo = guestNo;
    }

    /**
     *
     * @param lastName
     */
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    /**
     *
     * @param middleName
     */
    public void setMiddleName(String middleName) {
        this.middleName = middleName;
    }

    /**
     *
     * @param passportNo
     */
    public void setPassportNo(String passportNo) {
        this.passportNo = passportNo;
    }

    /**
     *
     * @param prefix
     */
    public void setPrefix(Prefix prefix) {
        this.prefix = prefix;
    }

    /**
     *
     * @param lastRoom
     */
    public void setLastRoom(Room lastRoom) {
        this.lastRoom = lastRoom;
    }

    public Address getAddress() {
        return address;
    }

    public Company getCompany() {
        return company;
    }

    public Room getLastRoom() {
        return lastRoom;
    }

    public String getPassportNo() {
        return passportNo;
    }

    public Prefix getPrefix() {
        return prefix;
    }

    /**
     *
     * @return
     */
    public String getFirstName() {
        return firstName;
    }

    /**
     *
     * @return
     */
    public String getMiddleName() {
        return middleName;
    }

    /**
     *
     * @return
     */
    public String getLastName() {
        return lastName;
    }

    /**
     *
     * @return
     */
    public String getEmail() {
        return email;
    }

    /**
     *
     * @param email
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     *
     * @return
     */
    public String getPhone1No() {
        return phone1No;
    }

    /**
     *
     * @param phone1No
     */
    public void setPhone1No(String phone1No) {
        this.phone1No = phone1No;
    }

    /**
     *
     * @return
     */
    public String getPhone2No() {
        return phone2No;
    }

    /**
     *
     * @param phone2No
     */
    public void setPhone2No(String phone2No) {
        this.phone2No = phone2No;
    }

    /**
     * Method for deleting the booking from the database
     */
    public void delete() {
        try {
            DbCon.getInstance().executeUpdate("DELETE FROM guest WHERE guestNo=" + guestNo);
        } catch (SQLException ex) {
            new EventMessage("SQL error in Guest.delete()", "There was an error performing the sql inquery\n").showException(ex);
        }
    }

    /**
     *
     * @return
     */
    public void insert() {
        try {
            if (businessGuest) {
                guestNo = DbCon.getInstance().insertAutoKey("INSERT INTO guest(prefixNo, firstName, middleName, lastName, addressNo, passportNo, email, phone1No, phone2No, isBusinessGuest, companyNo) VALUES (" + prefix.getPrefixNo() + ", '" + firstName + "', '" + middleName + "', '" + lastName + "', " + address.getAddressNo() + ", '" + passportNo + "', '" + email + "', '" + phone1No + "', '" + phone2No + "', " + businessGuest + ", " + company.getCompanyNo() + ")");
            } else {
                guestNo = DbCon.getInstance().insertAutoKey("INSERT INTO guest(prefixNo, firstName, middleName, lastName, addressNo, passportNo, email, phone1No, phone2No, isBusinessGuest) VALUES (" + prefix.getPrefixNo() + ", '" + firstName + "', '" + middleName + "', '" + lastName + "', " + address.getAddressNo() + ", '" + passportNo + "', '" + email + "', '" + phone1No + "', '" + phone2No + "', " + businessGuest + ")");
            }
        } catch (SQLException ex) {
            new EventMessage("SQL error in Guest.insert()", "There was an error performing the sql inquery\n").showException(ex);
        }
    }

    /**
     *
     * @return
     */
    @Override
    public String toString() {
        String name = "";
        if (middleName == null) {
            name = firstName + " " + lastName;
        } else {
            name = firstName + " " + middleName.substring(0, 1) + " " + lastName;
        }
        return name;
    }
}
